package com.spring.oaassist.web.controller;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

import org.springframework.stereotype.Controller;
import org.springframework.validation.BindingResult;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.google.common.collect.Maps;
import com.spera.shopoa.util.ShopOaSessionUtil;
import com.spera.shopoa.util.ShopOaSessionUtil.OaUser;
import com.spring.oaassist.web.model.Task;
import com.spring.oaassist.web.service.TaskService;
import com.spring.security.core.annotation.OaUserPermission;
import com.spring.security.core.util.ApplicationUtils;
import com.spring.security.core.validators.Delete;
import com.spring.security.core.validators.Insert;
import com.spring.security.core.validators.Receive;
import com.spring.security.core.validators.Update;
import com.spring.tools.Result;
import com.spring.tools.ValidatorResultHandler;

@Controller
@RequestMapping(value = "/task")
public class TaskController {
	@Resource private TaskService taskService;
	
	/**
	 * 主页
	 * @return
	 */
	@OaUserPermission(value="/task/index")
	@RequestMapping(value = "/index")
	public ModelAndView index(){
		return new ModelAndView("main/task/index");
	}
	
	/**
	 * 分派页面日志界面
	 * @return
	 */
	@RequestMapping(value = "/distribute")
	public ModelAndView distribute(String id){
		List<Map<String, Object>> m = taskService.queryDistributeList(id);
		Map<String,Object> retM = Maps.newHashMap();
		retM.put("logList", m); 
		return new ModelAndView("main/task/distribute",retM);
	}
	
	/**
	 * 我的日程
	 * @param id
	 * @return
	 */
	@OaUserPermission(value="/task/taskDaily")
	@RequestMapping(value = "/taskDaily")
	public ModelAndView taskDaily(){
		return new ModelAndView("main/workschedule/taskdaily");
	}
	
	/**
	 * 查询
	 * @param page
	 * @param pageSize
	 * @param request
	 * @return
	 */
	@SuppressWarnings({ "unchecked", "rawtypes" })
	@RequestMapping(value = "/queryTaskList")
    @ResponseBody
    public Object queryTaskList(String page,String pageSize,HttpServletRequest request){
		OaUser oaUser = ShopOaSessionUtil.getOaUser(request);
		Map<String, Object> retD = new HashMap<String, Object>();
		if(ApplicationUtils.isNotNull(oaUser.getOa_user_id())){
			Map<String, Object> parm = new HashMap<String, Object>();
	        parm.put("user_id", oaUser.getOa_user_id());
	    	PageHelper.startPage(Integer.parseInt(page), Integer.parseInt(pageSize));
	        List<Task> list = taskService.queryTaskList(parm);
	        PageInfo pageObj = new PageInfo(list);
	        retD.put("total", pageObj.getTotal());
	        retD.put("data", list);
		}
        return retD;
    }
	
	/**
	 * 根据登录用户查询用户日程表
	 * @param queryDate
	 * @param request
	 * @return
	 */
	@RequestMapping(value = "/queryTaskDaily")
    @ResponseBody
    public Object queryTaskDaily(String queryDate,HttpServletRequest request){
		OaUser oaUser = ShopOaSessionUtil.getOaUser(request);
		List<Map<String,Object>> list = null;
		if(ApplicationUtils.isNotNull(oaUser.getOa_user_id())){
			Map<String, Object> parm = new HashMap<String, Object>();
			parm.put("user_id", oaUser.getOa_user_id());
	        parm.put("queryDate", queryDate);
	        list = taskService.queryTaskDaily(parm);
		}
        return list;
    }
	
//	/**
//	 * 查询子任务
//	 * @param kids_park_id
//	 * @param page
//	 * @param pageSize
//	 * @param request
//	 * @return
//	 */
//	@SuppressWarnings({ "unchecked", "rawtypes" })
//	@RequestMapping(value = "/queryChildTaskList")
//    @ResponseBody
//    public Object queryChildTaskList(String task_id,String page,String pageSize,HttpServletRequest request){
////		OaUser oaUser = ShopOaSessionUtil.getOaUser(request);
//        Map<String, Object> parm = new HashMap<String, Object>();
//        parm.put("p_task_id", task_id);
//        Map<String, Object> retD = new HashMap<String, Object>();
//    	PageHelper.startPage(Integer.parseInt(page), Integer.parseInt(pageSize));
//        List<Task> list = taskService.queryTaskList(parm);
//        PageInfo pageObj = new PageInfo(list);
//        retD.put("total", pageObj.getTotal());
//        retD.put("data", list);
//        return retD;
//    }
	
	/**
	 * 新增子任务（任务分配）
	 * @param task
	 * @param result
	 * @param request
	 * @return
	 */
	@RequestMapping(value = "/addChildrenTask")
    @ResponseBody
    public Object addChildrenTask(@RequestBody @Validated({Insert.class}) Task task,BindingResult result,HttpServletRequest request){//
		OaUser oaUser = ShopOaSessionUtil.getOaUser(request);
		Result retVal = ValidatorResultHandler.handle(result);
		if("0".equalsIgnoreCase(retVal.getStatus())){
			return retVal;
		}
		task.setApplyDeptID(oaUser.getUser_dept_id());
		task.setApplyDeptName(oaUser.getUser_dept_name());
		task.setApplyUserID(oaUser.getOa_user_id());
		task.setApplyUserName(oaUser.getOa_user_name());
		retVal = taskService.insertTask(task);
        return retVal;
    }
	
	/**
	 * 删除任务
	 * @param task
	 * @param result
	 * @param request
	 * @return
	 */
	@RequestMapping(value = "/deleteTask")
    @ResponseBody
    public Object deleteTask(@RequestBody @Validated({Delete.class}) Task task,BindingResult result,HttpServletRequest request){//
		Result retVal = ValidatorResultHandler.handle(result);
		if("0".equalsIgnoreCase(retVal.getStatus())){
			return retVal;
		}
		try{
			retVal = taskService.deleteTask(task);
		}catch(Exception e){
			e.printStackTrace();
			retVal.setStatus("0");
			retVal.setMsg("删除异常，可能已被其他模块引用");
		}
        return retVal;
    }
	
	/**
	 * 接受或拒绝任务
	 * @param id
	 * @param reason
	 * @param type
	 * @param request
	 * @return
	 */
	@RequestMapping(value = "/updateTask")
    @ResponseBody
    public Object updateTask(@RequestBody @Validated({Receive.class}) Task task,BindingResult result,HttpServletRequest request){
		OaUser oaUser = ShopOaSessionUtil.getOaUser(request);
		task.setOperatorID(oaUser.getOa_user_id());
		task.setOperatorName(oaUser.getOa_user_name());
		Result retVal = ValidatorResultHandler.handle(result);
		if("0".equalsIgnoreCase(retVal.getStatus())){
			return retVal;
		}
		try{
			retVal = taskService.updateTask(task);
		}catch(Exception e){
			e.printStackTrace();
			retVal.setStatus("0");
			retVal.setMsg("删除异常，可能已被其他模块引用");
		}
        return retVal;
    }
	
	/**
	 * 结束任务
	 * @param task
	 * @param result
	 * @param request
	 * @return
	 */
	@RequestMapping(value = "/overTask")
    @ResponseBody
    public Object overTask(@RequestBody @Validated({Update.class}) Task task,BindingResult result,HttpServletRequest request){
		OaUser oaUser = ShopOaSessionUtil.getOaUser(request);
		task.setOperatorID(oaUser.getOa_user_id());
		task.setOperatorName(oaUser.getOa_user_name());
		Result retVal = ValidatorResultHandler.handle(result);
		if("0".equalsIgnoreCase(retVal.getStatus())){
			return retVal;
		}
		try{
			retVal = taskService.overTask(task);
		}catch(Exception e){
			e.printStackTrace();
			retVal.setStatus("0");
			retVal.setMsg("操作异常");
		}
        return retVal;
    }
}
